/**
 * 
 */
package com.ffcs.crmd.tsp.server.job.service;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.core.JobRunShellTsp;
import org.quartz.impl.tsp.ITspProcessJob;

import com.ffcs.crmd.tsp.api.dto.enu.JobType;
import com.ffcs.crmd.tsp.api.dto.job.TspJobExecutionContext;
import com.ffcs.crmd.tsp.api.dto.task.TspJobDataMap;
import com.ffcs.crmd.tsp.api.dto.task.TspJobInstance;
import com.ffcs.crmd.tsp.core.util.MixAll;
import com.ffcs.crmd.tsp.server.application.ApplicationManagerHolder;
import com.ffcs.crmd.tsp.server.job.info.JobDetailShell;
import com.ffcs.crmd.tsp.server.manager.JobProcessManager;
import com.ffcs.crmd.tsp.server.util.JobDetailHelper;

/**
 * 功能说明:触发quartz任务入口
 *
 * @author ZHONGFUHUA
 * 
 * @since 2.0.0-SNAPSHOT
 *
 */
public class JobProcessFromQuartz implements ITspProcessJob {
    
    /**
     * 功能说明:处理quartz任务内核
     * 
     * @param jobRunShell
     */
    @Override
    public void run(JobRunShellTsp jobRunShell) {
        JobExecutionContext context = jobRunShell.getJec();
        JobDetail jobDetail = context.getJobDetail();
        Trigger trigger = context.getTrigger();
        
        // 构建TspJobDetail对象
        TspJobInstance tspJobDetail = null;
        if (trigger instanceof SimpleTrigger) {
            SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
            tspJobDetail = JobDetailHelper.convert2TspJobDetail(jobDetail, simpleTrigger);
        } else if (trigger instanceof CronTrigger) {
            CronTrigger cronTrigger = (CronTrigger) trigger;
            tspJobDetail = JobDetailHelper.convert2TspJobDetail(jobDetail, cronTrigger);
        }
        
        if (jobDetail.getJobClass().equals(MixAll.TSP_STATEFUL_JOB)) {
            tspJobDetail.setJobType(JobType.STATEFUL);
        }
        
        TspJobDataMap tspJobDataMap = JobDetailHelper.convert2TspJobDataMap(context.getMergedJobDataMap());
        Integer priority = tspJobDataMap.getIntFromString(MixAll.TSP_JOB_PRIORITY);
        tspJobDetail.setJobClass(tspJobDataMap.getStringFromString(MixAll.TSP_JOB_CLASS));
        tspJobDetail.setPriority(priority == null ? 1
            : priority.intValue());
        tspJobDetail.setJobKey(jobDetail.getKey().toString());
        
        String schedulerName = null;
        String schedulerInstanceId = null;
        try {
            schedulerName = context.getScheduler().getSchedulerName();
            schedulerInstanceId = context.getScheduler().getSchedulerInstanceId();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        
        // 构建TspJobExecutionContext对象
        TspJobExecutionContext<Object> tspJobExecutionContext = new TspJobExecutionContext<Object>();
        tspJobExecutionContext.setFireInstanceId(schedulerInstanceId);
        tspJobExecutionContext.setFireTime(context.getFireTime());
        tspJobExecutionContext.setNextFireTime(context.getNextFireTime());
        tspJobExecutionContext.setPreviousFireTime(context.getPreviousFireTime());
        tspJobExecutionContext.setRecovering(context.isRecovering());
        tspJobExecutionContext.setScheduledFireTime(context.getScheduledFireTime());
        tspJobExecutionContext.setTspJobInstance(tspJobDetail);
        tspJobExecutionContext.setTspJobDataMap(tspJobDataMap);
        tspJobExecutionContext.setSchedulerName(schedulerName);
        
        JobDetailShell jobDetailRunInfo = new JobDetailShell(jobRunShell, tspJobExecutionContext);
        JobProcessManager jobProcessManager = ApplicationManagerHolder.getBean("jobProcessManager", JobProcessManager.class);
        jobProcessManager.putJobDetail(tspJobDetail.getJobName(), jobDetailRunInfo);
    }
}
